<?php

namespace App\Console\Commands;

use App\Api\Models\LogInStorageModel;
use App\Api\Models\PackageContentModel;
use App\Api\Models\StorageManageModel;
use App\V3\Api\Models\StorageShelfLocationModel;
use App\V3\Api\Models\StorageShelfModel;
use App\V3\Api\Models\VipOrderModel;
use App\V3\Api\Services\ShelfService;
use App\V3\V3_1\Models\SalesReturnGoodsDetailModel;
use App\V3\V3_1\Models\StorageModel;
use App\V3\V3_2_6\Models\LogOutStorageModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class update_shelf_data extends Command
{
    //php artisan shelf:create

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shelf:updateData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新相关表数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        set_time_limit(0);
        @ini_set('memory_limit', '1024M');
//            self::update_sales_return_goods_detail();
        self::update_storage_log();
        self::update_storage_out();

//        if(self::update_storage()){
//            self::update_storage_log();
//            self::update_package_content();
//            self::update_sales_return_goods_detail();
//        }
    }

    //更新库存表中的信息 添加默认仓库 storage
    public static function update_storage()
    {
        $list = StorageManageModel::where(['delete_flag' => 0])->get();
        foreach ($list as $k => $v){
            if(empty($v['shelf_id']) || empty($v['location_id'])){
                //查询货架和库位信息
                $shelf_data = ShelfService::get_default($v['warehouse_id']);
                if(!empty($shelf_data)){
                    $shelf_id = $shelf_data['id'];
                    $location_id = ShelfService::get_default($shelf_id,2)['id'];

                }else{
                    $shelf_id = 0;
                    $location_id = 0;
                }

                StorageManageModel::where('id', $v['id'])
                ->update(['shelf_id' => $shelf_id,'location_id' => $location_id]);

                echo '已完成-'.$v['id'].PHP_EOL;

            }
        }

        return true;
    }

    //更新库存操作日志信息 log_instorage
    public static function update_storage_log()
    {
        $list = LogInStorageModel::where([
            'delete_flag' => 0,
            'is_cancel' => 0
        ])
            ->where('location_id', 0)
            ->get();

        foreach ($list as $k => $v){
            if(empty($v['location_id'])){
                //查询货架和库位信息
                $shelf_data = ShelfService::get_default($v['storage_id']);

                if(!empty($shelf_data)){
                    $shelf_id = $shelf_data['id'];
                    $location_id = ShelfService::get_default($shelf_id,2)['id'];

                }else{
                    $shelf_id = 0;
                    $location_id = 0;
                }

                LogInStorageModel::where('id', $v['id'])
                    ->update(['shelf_id' => $shelf_id,'location_id' => $location_id]);

                echo '已完成aaa-'.$v['id'].PHP_EOL;

            }
        }
        return true;
    }

    public static function update_storage_out()
    {
        $list = LogOutStorageModel::where([
            'delete_flag' => 0,
            'is_cancel' => 0
        ])
            ->where('location_id', 0)
            ->get();

        foreach ($list as $k => $v){
            if(empty($v['location_id'])){
                //查询货架和库位信息
                $shelf_data = ShelfService::get_default($v['storage_id']);

                if(!empty($shelf_data)){
                    $shelf_id = $shelf_data['id'];
                    $location_id = ShelfService::get_default($shelf_id,2)['id'];

                }else{
                    $shelf_id = 0;
                    $location_id = 0;
                }

                LogOutStorageModel::where('id', $v['id'])
                    ->update(['shelf_id' => $shelf_id,'location_id' => $location_id]);

                echo '已完成aaa-'.$v['id'].PHP_EOL;

            }
        }
        return true;
    }

    //更新套餐列表 增加货架和库位 package_content
    public static function update_package_content()
    {
        $package_list = PackageContentModel::select('*')
            ->where('delete_flag',0)
            ->get();

        foreach ($package_list as $v){
            if(empty($v['shelf_id']) || empty($v['location_id'])){
                $info = StorageManageModel::find($v['storage_manage_id']);
                if(!empty($info)){
                    $shelf_id = $info['shelf_id'];
                    $location_id = $info['location_id'];

                    PackageContentModel::where('id', $v['id'])
                        ->update(['shelf_id' => $shelf_id,'location_id' => $location_id]);

                    echo '已完成-'.$v['id'].PHP_EOL;
                }
            }

        }
        return true;
    }

    //更新退货商品明细表  增加货架和库位 s_sales_return_goods_detail  s_log_instorage s_sales_return_package_content s_sales_return_in_package_content s_log_instorage
    public static function update_sales_return_goods_detail()
    {
        $detail_list = SalesReturnGoodsDetailModel::select('*')->get();

        foreach ($detail_list as $k => $v){
            if(empty($v['shelf_id']) || empty($v['location_id'])){
                //查询货架和库位信息
                $shelf_data = ShelfService::get_default($v['warehouse_id']);
                if(!empty($shelf_data)){
                    $shelf_id = $shelf_data['id'];
                    $location_id = ShelfService::get_default($shelf_id,2)['id'];

                }else{
                    $shelf_id = 0;
                    $location_id = 0;
                }

                SalesReturnGoodsDetailModel::where('id', $v['id'])
                    ->update(['shelf_id' => $shelf_id,'location_id' => $location_id]);

                echo '已完成-'.$v['id'].PHP_EOL;

            }
        }
        return true;
    }


    //s_sell_goods_detail s_sell_package_content sell_out_storage s_log_outstorage s_sell_materiel_prepare_out_log s_log_package_content

    //s_materiel_content
}
